package com.sz.framework.widget;

import android.content.Context;
import android.os.Build;
import android.support.annotation.DrawableRes;
import android.support.annotation.Nullable;
import android.support.annotation.StringRes;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.blankj.utilcode.util.BarUtils;
import com.sz.framework.R;

import butterknife.ButterKnife;

/**
 * 自定义标题栏
 */
public class TitleManager extends LinearLayout {
    View vStatus;
    TextView tvTitle;
    ImageView ivBack;
    ImageView ivRight;

    public TitleManager(Context context) {
        this(context, null);
    }

    public TitleManager(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TitleManager(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        setOrientation(LinearLayout.VERTICAL);
        View contentView = LayoutInflater.from(context).inflate(R.layout.title_content, null);
        LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
        this.addView(contentView, layoutParams);
        ButterKnife.bind(contentView);
        // 设置状态栏高度 4.4+
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            vStatus = contentView.findViewById(R.id.v_status);
            int statusHeight = BarUtils.getStatusBarHeight();
            LayoutParams statusParams = new LayoutParams(LayoutParams.MATCH_PARENT, statusHeight);
            vStatus.setLayoutParams(statusParams);
        }
        tvTitle = findViewById(R.id.tv_title);
        ivBack = findViewById(R.id.iv_back);
        ivRight = findViewById(R.id.iv_right);
    }

    public void showBack(View.OnClickListener listener) {
        ivBack.setVisibility(View.VISIBLE);
        ivBack.setOnClickListener(listener);
    }

    public void showTitle(String title) {
        tvTitle.setVisibility(View.VISIBLE);
        tvTitle.setText(title);
    }

    public void showTitle(@StringRes int resId) {
        tvTitle.setVisibility(View.VISIBLE);
        tvTitle.setText(resId);
    }

    public void showRight(@DrawableRes int resId, View.OnClickListener listener){
        ivRight.setVisibility(View.VISIBLE);
        ivRight.setImageResource(resId);
        ivRight.setOnClickListener(listener);
    }
}
